<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/4/8
 * Time: 13:46
 */
namespace app\blog\controller;
use cmf\controller\AdminBaseController;
use app\blog\model\GoodsStateModel as good;
use app\portal\model\PortalCategoryModel;
use think\Db;

class GoodsstateController extends AdminBaseController
{
    /*
    * 获取登录信息
    * */
    public function session()
    {
        $session=cmf_get_current_admin_id();
        $where['id']=$session;
        $store=DB::name('user')->where($where)->field('store_id')->find();
        $store_id=$store['store_id'];
        return $store_id;
    }
    public function index()
    {
        $good=new good();
        $session=cmf_get_current_admin_id();
        $shop=$this->session($session);
        $where=array();
        if($shop !=0)
        {
            $where['id']=$shop;
        }
        $type=DB::name('store')->where($where)->select();
        $date=$good->good();
        // 获取分页显示
        $page = $date->render();
        $this->assign('data',$date);
        $this->assign('page',$page);
        $this->assign('stort',$type);
        return $this->fetch();
    }
    /*
     * 2018-05-31添加
     * */
    public function add()
    {
        $session=cmf_get_current_admin_id();
        $shop=$this->session($session);
        if ($this->request->isPost()){
                $res  = $this->request->param();
                $data['storeid']=$shop;
                $data['state_name']=$res['name'];
                $data['status']=$res['status'];
                $data['time']=time();
                $re=Db::name('goods_state')->insert($data);
                if($re){
                    $this->success("添加成功！",url("goods/index"));
                }else{
                 return   $this->error("添加失败，请重新添加！", url('goodsstate/add'));
                }
            }else{
                return $this->fetch();
            }
    }
    /*
     * 2018 添加
     * */
    public function addpost()
    {
        $session=cmf_get_current_admin_id();
        $shop=$this->session($session);
        $res  = $this->request->param();
        $data['storeid']=$shop;
        $data['state_name']=$res['name'];
        $data['status']=$res['status'];
        $data['time']=time();
        $re=Db::name('goods_state')->insert($data);
        if($re){
            $this->success("添加成功！","goodsstate/index");
        }else{
            return   $this->error("添加失败，请重新添加！", url('goodsstate/add'));
            }
    }
    /*
     * 2018-05-31更新
     * */
    public function update()
    {
        if($this->request->isPost())
        {
             $data=$this->request->param();
             $where['id']=$data['sid'];
             $date['state_name']=$data['name'];
             $date['status']=$data['status'];
             $date['time']=time();
            $re=Db::name('goods_state')->where($where)->update($date);
            if($re){
                $this->success("修改成功！", url("goodsstate/index"));
            }else{
                $this->error("修改失败！", url("goodsstate/add"));
            }

        }else{
            $data=$this->request->param();
            $where['id']=$data['id'];
            $array=DB::name('goods_state')->where($where)->find();
            $this->assign('data',$array);
            return $this->fetch();
        }
    }
    public function del()
    {
        $data=$this->request->param();
        $state=DB::name('goods_state')->where($data)->delete();
        if($state)
        {
            return $this->success('删除成功');
        }else{
            return $this->success('删除失败');
        }
    }
}